package vashion.azeroth.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.management.loading.PrivateMLet;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.contant.PersonalMyInfoErrorEnum;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.query.ItemQueryDO;
import vashion.azeroth.core.manager.item.BaseItemManager;
import vashion.azeroth.core.manager.personal.PersonalInviteCodeManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.manager.personal.PersonalMemberVipManager;
import vashion.azeroth.core.manager.personal.PersonalProfilesManager;
import vashion.azeroth.core.manager.personal.PersonalPublishFilesManager;
import vashion.azeroth.core.manager.personal.tran.PersonalVAccountManager;
import vashion.azeroth.core.personal.pojo.PersonalAddress;
import vashion.azeroth.core.personal.pojo.PersonalHisInfo;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.pojo.PersonalMemberVip;
import vashion.azeroth.core.personal.pojo.PersonalMyInfo;
import vashion.azeroth.core.personal.pojo.PersonalProfiles;
import vashion.azeroth.core.personal.pojo.PersonalPublishFiles;
import vashion.azeroth.core.personal.tran.pojo.PersonalVAccount;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalAddressService;
import vashion.azeroth.core.service.PersonalFollowService;
import vashion.azeroth.core.service.PersonalMyInfoService;
import vashion.azeroth.feed.FeedTagsDo;

import com.alibaba.fastjson.JSONArray;

@Service
public class PersonalMyInfoServiceImpl<T> implements PersonalMyInfoService<T> {

	@Autowired
	private PersonalProfilesManager personalProfilesManager;

	@Autowired
	private PersonalMemberManager personalMemberManager;

	@Autowired
	private PersonalInviteCodeManager personalInviteCodeManager;

	@Autowired
	private PersonalPublishFilesManager personalPublishFilesManager;

	@Autowired
	private PersonalMemberVipManager personalMemberVipManager;

	@Autowired
	private BaseItemManager baseItemManager;
	
	@Autowired
	private PersonalVAccountManager personalVAccountManager;
	
	@Autowired
	private PersonalFollowService<T> personalFollowService;
	
	@Autowired
	private PersonalAddressService<T> personalAddressService; 

	public ApiResult<PersonalMyInfo> getMyInfo(PersonalMember member) {

		ApiResult<PersonalMyInfo> result = new ApiResult<PersonalMyInfo>();
		PersonalMyInfo resInfo = new PersonalMyInfo();
		try {
			resInfo.setMemberId(member.getMemberId());
			PersonalMember resMem = personalMemberManager.get(member
					.getMemberId());
			if (null == resMem) {
				return ApiResult.newErrorResult(
						MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
						MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
			}
			resInfo.setHeadImg(resMem.getHeadImg());
			resInfo.setNick(resMem.getNickName());
			resInfo.setMemberName(resMem.getMemberName());
			resInfo.setSex(resMem.getSex());

			if (null == resMem.getPayPwd() || resMem.getPayPwd().equals("")) {
				resInfo.setvCoinPwd(false);
			} else {
				resInfo.setvCoinPwd(true);
			}

			PersonalProfiles resProfile = personalProfilesManager.get(member
					.getMemberId());
			
			if (null == resProfile) {
				resInfo.setFansCnt(0);
				resInfo.setFollowCnt(0);
				resInfo.setCurrentExperiencePoint(0);
			}else {
				if (null == resProfile.getFansCount()) {
					resInfo.setFansCnt(0);
				} else {
					resInfo.setFansCnt(resProfile.getFansCount());
				}

				if (null == resProfile.getFollowCount()) {
					resInfo.setFollowCnt(0);
				} else {
					resInfo.setFollowCnt(resProfile.getFollowCount());
				}

				if (null == resProfile.getPointCount()) {
					resInfo.setCurrentExperiencePoint(0);
				} else {
					resInfo.setCurrentExperiencePoint(resProfile.getPointCount());
				}
			}

			PersonalMemberVip pmv = personalMemberVipManager.get(member
					.getMemberId());
			if (null == pmv || null == pmv.getVipLevel()) {
				resInfo.setLevelCnt(0);
			} else {
				//resInfo.setLevelCnt(Integer.valueOf(pmv.getVipLevel()));
			}

			Integer cntMyCode = personalInviteCodeManager
					.findCntByMemberId(member);
			if (null == cntMyCode) {
				resInfo.setInviteCnt(0);
			} else {
				resInfo.setInviteCnt(cntMyCode);
			}
			
			PersonalVAccount vAccount = personalVAccountManager.getByMemberId(member.getMemberId());
			if (null == vAccount) {
				resInfo.setvCoinBalanceQuantity(0);
			}else {
				resInfo.setvCoinBalanceQuantity(vAccount.getUseableBalance());
			}
			
			PersonalAddress addr = new PersonalAddress();
			addr.setMemberId(member.getMemberId());
			ApiResult<PersonalAddress> defaddr = personalAddressService.queryDefaultAddress(addr );
			if (null == defaddr.getData()) {
				resInfo.setDefaultAddrStauts(0);
				resInfo.setDefaultAddr(null);
			}else {
				resInfo.setDefaultAddrStauts(1);
				resInfo.setDefaultAddr(defaddr.getData());
			}
			
			result = ApiResult.newSuccessResult(resInfo);
		} catch (Exception e) {
			return ApiResult
					.newErrorResult(PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL
							.getErrorCode(),
							PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL
									.getErrorMessage());
		}
		return result;
	}

	public ApiResult<PersonalHisInfo> getHisInfo(PersonalHisInfo member) {
		ApiResult<PersonalHisInfo> result = new ApiResult<PersonalHisInfo>();
		PersonalHisInfo resInfo = new PersonalHisInfo();

		try {
			if (member.getHisMemberId() == member.getMyMemberId()) {
//				判断是否为自已
				resInfo.setFollowRelation(3);
			}else {
				
			ApiResult<Integer> apiResult = personalFollowService.isFollowed(
					member.getMyMemberId(), member.getHisMemberId());
			resInfo.setFollowRelation(apiResult.getData());
			}

			resInfo.setHisMemberId(member.getHisMemberId());
			PersonalMember resMem = personalMemberManager.get(member
					.getHisMemberId());
			if (null == resMem) {
				return vashion.azeroth.core.result.ApiResult.newErrorResult(
						MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
						MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
			}
			resInfo.setHeadImg(resMem.getHeadImg());
			resInfo.setNick(resMem.getNickName());
			resInfo.setMemberName(resMem.getMemberName());
			resInfo.setSex(resMem.getSex());

			PersonalMemberVip pmv = personalMemberVipManager.get(member
					.getHisMemberId());
			if (null == pmv || null == pmv.getVipLevel()) {
				resInfo.setLevelCnt(0);
			} else {
				resInfo.setLevelCnt(Integer.valueOf(pmv.getVipLevel()));
			}
			PersonalProfiles profiles = new PersonalProfiles();
			profiles.setMemberId(member.getHisMemberId());

			PersonalPublishFiles files = new PersonalPublishFiles();
			files.setMemberId(member.getHisMemberId());
			files.setFirstRow(0);
			files.setPageSize(100);
			ArrayList<PersonalPublishFiles> resFiles = personalPublishFilesManager
					.getFilesBymemberId(files);
			ArrayList<Long> itemList = new ArrayList<Long>();
			for (int i = 0; i < resFiles.size(); i++) {
				if (null == resFiles.get(i).getTags()
						|| resFiles.get(i).getTags().equals("[]")) {
					continue;
				} else {
					String tags = resFiles.get(i).getTags();
					JSONArray array = JSONArray.parseArray(tags);
					for (int j = 0; j < array.size(); j++) {
						ObjectMapper mapper = new ObjectMapper();
						FeedTagsDo ftlist = mapper.readValue(array.get(j)
								.toString(), FeedTagsDo.class);
						itemList.add(Long.valueOf(ftlist.getGoodID()));
					}
				}
			}

			ItemQueryDO itemQueryDO = new ItemQueryDO();
			itemQueryDO.setItemIdList(itemList);

			if (itemList.size() != 0) {
				// 根据itemid获取商品
				List<ItemDO> resItem = baseItemManager.queryItem(itemQueryDO);
				resInfo.setRecommendItems((ArrayList<ItemDO>) resItem);
			}
			result = ApiResult.newSuccessResult(resInfo);
		} catch (Exception e) {
			System.out.println(e);
			ApiResult
					.newErrorResult(PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL
							.getErrorCode(),
							PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL
									.getErrorMessage());
		}
		return result;
	}

}
